package cn.javabb.core.runner;

import cn.javabb.core.redis.RedisProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.ContextLoaderListener;

import javax.servlet.ServletContextEvent;

/** 程序启动后打印配置信息
 * @desc:
 * @author: javabb (javabob(a)163.com)
 * @create: 2019/12/23 11:14
 */
@Slf4j
@Component
public class BbsApplicationRunner extends ContextLoaderListener implements ApplicationRunner {

    @Value("${server.port}")
    private int port;
    @Value("${spring.profiles.active}")
    private String profile;
    @Value("${app.enabledConfigLog}")
    private Boolean enabledConfigLog;
    @Autowired
    private DataSourceProperties dataSourceProperties;
    @Autowired
    private RedisProperties redisProperties;
    @Autowired
    private ConfigurableApplicationContext configurableApplicationContext;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("Javabb-bbs部署完成，访问地址：http://127.0.0.1:" + port);
    }
    @Override
    public void contextInitialized(ServletContextEvent event) {
        if (null != enabledConfigLog && enabledConfigLog) {
            log.info("系统配置信息：");
            String[] activeProfiles = configurableApplicationContext.getEnvironment().getActiveProfiles();
            if (ObjectUtils.isEmpty(activeProfiles)) {
                String[] defaultProfiles = configurableApplicationContext.getEnvironment().getDefaultProfiles();
                log.info("No active profile set, falling back to default profiles: " + StringUtils.arrayToCommaDelimitedString(defaultProfiles));
            } else {
                log.info("The following profiles are active: " + StringUtils.arrayToCommaDelimitedString(activeProfiles));
            }

            log.info("Data Source：");
            log.info("  - url：{}", dataSourceProperties.getUrl());
            log.info("  - username：{}", dataSourceProperties.getUsername());
            log.info("  - password：{}", dataSourceProperties.getPassword());

            log.info("Redis：");
            log.info("  - database：{}", redisProperties.getDatabase());
            log.info("  - host：{}", redisProperties.getHost());
            log.info("  - port：{}", redisProperties.getPort());
            log.info("  - password：{}", redisProperties.getPassword());
        }
    }


}
